package classroom;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.ProgressDialog;
import android.graphics.PixelFormat;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;

import com.project.institute.DAO.InstituteList;
import com.project.trac.R;

import core.modules.DatabaseConnector;

public class OverAllRating extends InstituteList {
	
	private String PG;
	private String MG;
	private String FG;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.generic_list);
		
		new AsynchronousTask().execute();
	
	}
	
	@Override
	public void onAttachedToWindow() {
		super.onAttachedToWindow();
		Window window = getWindow();
		window.setFormat(PixelFormat.RGBA_8888);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case R.id.menu_logout:
	           Logout(this);
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	private class AsynchronousTask extends AsyncTask<String, String, String> {

		HashMap<String, String> m;
		ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>();

		ProgressDialog progressDialog;
		ListAdapter listAdapter;

		String KEY_SUBJECT_HEADER = "k1";
		String KEY_CONTENT = "k2";
		String KEY_CONTENT3 = "k3";
		String KEY_CONTENT4 = "k4";
		
		protected void onPreExecute() {
			super.onPreExecute();
			progressDialog = new ProgressDialog(OverAllRating.this);
			progressDialog.setIcon(android.R.drawable.ic_dialog_info);
			progressDialog.setMessage("please wait...");
			progressDialog.setIndeterminate(true);
			progressDialog.setCancelable(true);
			progressDialog.show();

		}

		protected String doInBackground(String... args) {
			try {

				ResultSet rs = DatabaseConnector.getStatement().executeQuery(
						" SELECT gradesheet.uid, gradesheet.prelim, gradesheet.midterm, gradesheet.final, curriculum.catalognumber"
						+ " FROM gradesheet"
						+ " INNER JOIN curriculum ON curriculum.id = gradesheet.cid"
						+ " WHERE gradesheet.uid = " + getUserID() + " AND isApproved = 1");

				while (rs.next()) {

					PG = rs.getString("prelim");
					MG = rs.getString("midterm");
					FG = rs.getString("final");

					m = new HashMap<String, String>();
					m.put(KEY_SUBJECT_HEADER, rs.getString("catalognumber"));
					m.put(KEY_CONTENT, PG);
					m.put(KEY_CONTENT3, MG);
					m.put(KEY_CONTENT4, FG);
					arrayList.add(m);

				}

			} catch (SQLException e) {
				DatabaseConnector.Logger(getClass(), "doInBackground", e);
			} 

			return null;
		}

		protected void onPostExecute(String postExecute) {
			progressDialog.dismiss();
			
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					
					listAdapter = new SimpleAdapter(OverAllRating.this, arrayList,
							R.layout.commons_rating, 
							new String[] { KEY_SUBJECT_HEADER, KEY_CONTENT, KEY_CONTENT3, KEY_CONTENT4 }, 
							new int[] {	R.id.rating_subjectHeader, R.id.actualPG, R.id.actualMG, R.id.actualFG});
					setListAdapter(listAdapter);
					
				}

			});

		}

	}

}